<template>
  <div class="app-container home">
    <el-row :gutter="20" type="flex" justify="end" class="mb20">
      <!-- <el-col :span="8">{{w}}*{{h}}</el-col> -->
      <el-col :span="8">
        <el-select
          v-model="area"
          placeholder="请选择供电公司"
          clearable
          @change="getData"
        >
          <el-option
            v-for="dict in areaOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="box-content">
      <div
        class="box"
        style="background: rgb(144, 113, 184);"
        @click="toPage('station')"
      >
        <div class="left">
          <!-- <img src="@/assets/icons/index/u59.png" alt="" srcset="" /> -->
          <i class="iconfont iconthin-_home_flat"></i>
        </div>
        <div class="right">
          <div class="top">{{ dataSum.station || 0 }}</div>
          <div class="bottom">站点</div>
        </div>
      </div>
      <div
        class="box"
        style="background: rgb(109, 117, 204);"
        @click="toPage('opticalcable')"
      >
        <div class="left">
          <!-- <img src="@/assets/icons/index/u64.png" alt="" srcset="" /> -->
          <i class="iconfont icontongxunxianlan"></i>
        </div>
        <div class="right">
          <div class="top">{{ dataSum.opticalCable || 0 }}</div>
          <div class="bottom">光缆</div>
        </div>
      </div>
      <div
        class="box"
        style="background: rgb(91, 168, 194);"
        @click="toPage('machineRoom')"
      >
        <div class="left">
          <!-- <img src="@/assets/icons/index/u65.png" alt="" srcset="" /> -->
          <i class="iconfont iconjifang2"></i>
        </div>
        <div class="right">
          <div class="top">{{ dataSum.machineRoom || 0 }}</div>
          <div class="bottom">机房</div>
        </div>
      </div>
      <div
        class="box"
        style="background: rgb(91, 165, 152);"
        @click="toPage('machineCabinet')"
      >
        <div class="left">
          <!-- <img src="@/assets/icons/index/u60.png" alt="" srcset="" /> -->
          <i class="iconfont iconjifang1"></i>
        </div>
        <div class="right">
          <div class="top">{{ dataSum.machineCabinet || 0 }}</div>
          <div class="bottom">机柜</div>
        </div>
      </div>
      <div
        class="box"
        style="background: rgb(190, 103, 126);"
        @click="toPage('equipment')"
      >
        <div class="left">
          <!-- <img src="@/assets/icons/index/u61.png" alt="" srcset="" /> -->
          <i class="iconfont iconjifangsheshi"></i>
        </div>
        <div class="right">
          <div class="top">{{ dataSum.equipment || 0 }}</div>
          <div class="bottom">设备</div>
        </div>
      </div>
      <div
        class="box"
        style="background: rgb(190, 103, 106);"
        @click="toPage('frame')"
      >
        <div class="left">
          <!-- <img src="@/assets/icons/index/u66.png" alt="" srcset="" /> -->
          <i class="iconfont iconjifang3"></i>
        </div>
        <div class="right">
          <div class="top">{{ dataSum.distributionFrame || 0 }}</div>
          <div class="bottom" style="width: 100px; margin-left: -15px;">配线模块</div>
        </div>
      </div>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="24"><div class="baidumap" id="allmap"></div></el-col>
    </el-row>

    <!-- 添加或修改站点对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      :width="getDialogWidth()"
      append-to-body
    >
      <el-form
        ref="form"
        :size="'medium'"
        :model="form"
        :rules="rules"
        label-width="125px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="站点名称" prop="stationName">
              <el-input
                v-model="form.stationName"
                placeholder="请输入站点名称"
                suffix-icon="xxx"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="站点类型" prop="stationType">
              <el-select
                v-model="form.stationType"
                placeholder="请选择站点类型"
                clearable
              >
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="电压等级" prop="kv">
              <el-select
                v-model="form.kv"
                placeholder="请选择电压等级"
                clearable
              >
                <el-option
                  v-for="dict in KVOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供电公司" prop="area">
              <el-select
                v-model="form.area"
                placeholder="请选择供电公司"
                clearable
              >
                <el-option
                  v-for="dict in areaOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="tel">
              <el-input
                v-model="form.tel"
                placeholder="请输入联系电话"
                suffix-icon="xxx"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="光缆出口数量" prop="opticalOutNum">
              <el-input-number
                v-model="form.opticalOutNum"
                placeholder="请输入"
                suffix-icon="xxx"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="经纬度" prop="longitude">
              <el-input
                v-model="form.longitude"
                placeholder="请输入经度"
                suffix-icon="xxx"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6" class="ml10">
            <el-form-item label-width="0" prop="latitude">
              <el-input
                v-model="form.latitude"
                placeholder="请输入纬度"
                suffix-icon="xxx"
              />
            </el-form-item>
          </el-col>
          <el-col :span="2" class="ml20">
            <el-button
              type="primary"
              icon="el-icon-location"
              @click="openSelectPoint"
            >
              获取定位
            </el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="传输网上联速率" prop="networkRate">
              <el-select
                v-model="form.networkRate"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="dict in rateOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="传输网下联带宽" prop="networkBw">
              <el-select
                v-model="form.networkBw"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="dict in bwOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="产权单位" prop="propertyUnit">
              <el-input
                v-model="form.propertyUnit"
                placeholder="请输入产权单位"
                suffix-icon="xxx"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="danger"
          plain
          @click="handleDelete"
          v-if="title == '修改站点'"
          v-hasPermi="['system:station:remove']"
        >
          删 除
        </el-button>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm" style="width: 120px;">
          确 定
        </el-button>
      </div>
    </el-dialog>

    <el-drawer
      title="选择坐标"
      :with-header="false"
      :visible.sync="drawer"
      direction="btt"
      :destroy-on-close="true"
      :show-close="false"
      size="50%"
    >
      <div class="drawer-content">
        <div class="drawerBaiduMap" id="drawerMap"></div>
        <img
          class="centerMarker"
          src="../assets/images/marker.png"
          alt=""
          srcset=""
        />
        <div class="enter">
          <el-button type="primary" @click="enterGeo">确定</el-button>
        </div>
        <div class="close">
          <el-button type="info" @click="drawerClose">取消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  getStation,
  addStation,
  delStation,
  updateStation,
} from '@/api/station/station'
import { getHomePageData } from '@/api/index/index.js'
import { checkPermi, checkRole } from '@/utils/permission'
export default {
  name: 'index',
  data() {
    return {
      // w:document.body.clientWidth,
      // h:document.body.clientHeight ,
      area: '',
      areaOptions: [],
      dataSum: {},
      map: null,
      // 表单校验
      rules: {
        stationName: [
          { required: true, message: '请输入站点名称', trigger: 'change' },
        ],
        stationType: [
          { required: true, message: '请选择站点类型', trigger: 'change' },
        ],
        area: [{ required: true, message: '供电公司', trigger: 'change' }],
        kv: [{ required: true, message: '请选择电压等级', trigger: 'change' }],
        tel: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            required: true,
            message: '请检查电话格式',
            trigger: 'blur',
          },
          { required: true, message: '请输入联系电话', trigger: 'change' },
        ],
        opticalOutNum: [
          { required: true, message: '请输入光缆出口数量', trigger: 'change' },
        ],
        longitude: [
          { required: true, message: '请输入经度', trigger: 'change' },
        ],
        latitude: [
          { required: true, message: '请输入纬度', trigger: 'change' },
        ],
        networkRate: [
          {
            required: true,
            message: '请选择传输网上联速率',
            trigger: 'change',
          },
        ],
        networkBw: [
          {
            required: true,
            message: '请选择传输网下联带宽',
            trigger: 'change',
          },
        ],
        propertyUnit: [
          { required: true, message: '请输入产权单位', trigger: 'change' },
        ],
      },
      drawer: false,
      title: '',
      drawerMap: null,
      selectLngLat: {},
      //调度等级
      levelOptions: [],
      //电压等级
      KVOptions: [],
      //上传速率
      rateOptions: [],
      //下载速率
      bwOptions: [],
      statusOptions: [],
      form: {},
      open: false,
    }
  },
  mounted() {
    this.getDicts('station_type').then((response) => {
      this.statusOptions = response.data
    })
    this.getDicts('area').then((response) => {
      this.areaOptions = [
        { dictValue: '', dictLabel: '全部地区' },
        ...response.data,
      ]
    })
    this.getDicts('dispatch_level').then((response) => {
      this.levelOptions = response.data
    })
    this.getDicts('KV').then((response) => {
      this.KVOptions = response.data
    })
    this.getDicts('network_rate').then((response) => {
      this.rateOptions = response.data
    })
    this.getDicts('network_bw').then((response) => {
      this.bwOptions = response.data
    })
    this.getData()
  },
  activated() {
    this.getDicts('station_type').then((response) => {
      this.statusOptions = response.data
    })
    this.getDicts('area').then((response) => {
      this.areaOptions = [
        { dictValue: '', dictLabel: '全部地区' },
        ...response.data,
      ]
    })
    this.getDicts('dispatch_level').then((response) => {
      this.levelOptions = response.data
    })
    this.getDicts('KV').then((response) => {
      this.KVOptions = response.data
    })
    this.getDicts('network_rate').then((response) => {
      this.rateOptions = response.data
    })
    this.getDicts('network_bw').then((response) => {
      this.bwOptions = response.data
    })
    this.getData()
  },
  methods: {
    toPage(name) {
      switch (true) {
        case name == 'station':
          if (checkPermi(['system:station:list'])) {
            this.$router.push({ path: '/station' })
          } else {
            this.msgError('您没有查看该菜单的权限')
          }
          break
        case name == 'opticalcable':
          if (checkPermi(['system:cable:list'])) {
            this.$router.push({ path: '/opticalcable' })
          } else {
            this.msgError('您没有查看该菜单的权限')
          }
          break
        case name == 'machineRoom':
          if (checkPermi(['system:room:list'])) {
            this.$router.push({ path: '/machineRoom' })
          } else {
            this.msgError('您没有查看该菜单的权限')
          }
          break
        case name == 'machineCabinet':
          if (checkPermi(['system:cabinet:list'])) {
            this.$router.push({ path: '/cabinet' })
          } else {
            this.msgError('您没有查看该菜单的权限')
          }
          break
        case name == 'equipment':
          if (checkPermi(['system:equipment:list'])) {
            this.$router.push({ path: '/equipment' })
          } else {
            this.msgError('您没有查看该菜单的权限')
          }
          break
        case name == 'frame':
          if (checkPermi(['system:frame:list'])) {
            this.$router.push({ path: '/frame' })
          } else {
            this.msgError('您没有查看该菜单的权限')
          }
          break
        default:
          break
      }
    },
    /** 删除按钮操作 */
    handleDelete() {
      const stationName = this.form.stationName
      const ids = this.form.id
      this.$confirm(
        '是否确认删除站点为"' + stationName + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        },
      )
        .then(function () {
          return delStation(ids)
        })
        .then(() => {
          this.cancel()
          this.getData()
          this.msgSuccess('删除成功')
        })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateStation(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getData()
            })
          } else {
            addStation(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getData()
            })
          }
        }
      })
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getStation(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改站点'
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stationName: null,
        stationType: null,
        area: null,
      }
      this.resetForm('form')
    },
    openSelectPoint() {
      this.drawer = true
      let _this = this
      this.$nextTick(() => {
        // _this.drawerMap = new BMap.Map('drawerMap') // 创建地图实例
        // _this.drawerMap.enableScrollWheelZoom(true) //开启鼠标滚轮缩放
        _this.drawerMap.addEventListener('dragend', function showInfo() {
          var cp = _this.drawerMap.getCenter()
          _this.selectLngLat = {
            lng: cp.lng,
            lat: cp.lat,
          }
        })
        // var geolocation = new BMap.Geolocation()
        // geolocation.getCurrentPosition(function (r) {
        //   if (this.getStatus() == BMAP_STATUS_SUCCESS) {
        //     var point = new BMap.Point(r.point.lng, r.point.lat)
        //     _this.selectLngLat = {
        //     lng: r.point.lng,
        //     lat: r.point.lat,
        //   }
        //     _this.drawerMap.centerAndZoom(point, 15)
        //   } else {
        //     alert('failed' + this.getStatus())
        //   }
        // })
      })
    },
    enterGeo() {
      this.$set(this.form, 'longitude', this.selectLngLat.lng)
      this.$set(this.form, 'latitude', this.selectLngLat.lat)
      this.drawerClose()
    },
    drawerClose() {
      this.selectLngLat = {}
      this.drawer = false
    },
    getData() {
      let _this = this
      this.baiduMap()
      // this.map.clearOverlays()
      getHomePageData({ area: this.area || '' }).then((res) => {
        this.dataSum = res
        // if (res.stationInfo && res.stationInfo.length) {
        //   let points = []
        //   res.stationInfo.forEach((e) => {
        //     let marker = new BMap.Marker(
        //       new BMap.Point(e.longitude, e.latitude),
        //     )
        //     // points.push(new BMap.Point(e.longitude, e.latitude))
        //     // marker.customData = { id: e.id }
        //     // marker.addEventListener('click', function (e) {
        //     //   _this.handleUpdate(e.currentTarget.customData)
        //     // })
        //     // this.map.addOverlay(marker)
        //     // this.map.setViewport(points)
        //   })
        // }
      })
    },
    baiduMap() {
      // this.map = new BMap.Map('allmap') // 创建地图实例
      // this.map.enableScrollWheelZoom(true) //开启鼠标滚轮缩放
      // var point = new BMap.Point(116.404, 39.915)
      // this.map.centerAndZoom(point, 15)
    },
  },
}
</script>

<style scoped lang="scss">
.drawerBaiduMap {
  width: 100%;
  height: 100%;
}
.drawer-content {
  width: 100%;
  height: 100%;
  position: relative;
}
.centerMarker {
  position: absolute;
  top: calc(50% - 40px);
  left: calc(50% - 14px);
}

.enter {
  position: absolute;
  top: 20px;
  right: 20px;
}

.close {
  position: absolute;
  top: 20px;
  left: 20px;
}

.baidumap {
  width: 100%;
  height: calc(100vh - 300px);
}
.box-content {
  display: flex;
  display: -webkit-flex;
  justify-content: flex-start;
  flex-direction: row;
  flex-wrap: wrap;
}
.box {
  width: 145px;
  padding-left: 10px;
  padding-right: 10px;
  height: 105px;
  display: flex;
  margin-right: 10px;
  margin-bottom: 10px;
  .left {
    width: 40%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
   
    img {
      width: 42px;
      height: 42px;
    }
  }
  .right {
    width: 60%;
    height: 100%;
    color: #fff;
    display: flex;
    flex-direction: column;
    .top {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 42px;
      height: 75px;
    }
    .bottom {
      display: flex;
      align-items: flex-start;
      justify-content: center;
      font-size: 21px;
      height: 40px;
    }
  }
}
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100% !important;
}
.el-input-number--medium {
  width: 100% !important;
}
.ml20 {
  margin-left: 20px;
}
.h28 {
  height: 28px;
  margin-top: 3px;
}
</style>
